<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <div class="layui-carousel" id="test10">
                <div carousel-item>
                    <div><img src="images/img19.jpg" style="object-fit:scale-down;"></div>
                    <div><img src="images/loginbg.png" style="object-fit:scale-down;"></div>
                    <div><img src="images/img26.jpg" style="object-fit:scale-down;"></div>
                </div>
            </div>
            <legend>搜索用户</legend>
            <div style="margin: 10px 10px 10px 10px">
                <div class="demoTable" style="float:right">
                    <label class="layui-form-label">搜索条件</label>
                    <div class="layui-input-inline">
                        <select name="condition" id="select"
                                style="height: 30px;background-color: #bfdce5;border-radius: 5px">
                            <option value="">请选择</option>
                            <option value="1">编号</option>
                            <option value="2">账号</option>
                            <option value="3">昵称</option>
                            <option value="4">手机号</option>
                        </select>
                    </div>
                    <div class="layui-input-inline">
                        <input class="layui-input" style="border-radius: 15px;" name="num" id="demoReload"
                               autocomplete="off">
                    </div>
                    <button class="layui-btn" style="border-radius: 15px;background-color: rgb(255,255,255)"
                            data-type="reload">
                        <i class="layui-icon layui-icon-search" style="font-size: 30px; color: #429adc;"></i></button>
                </div>

            </div>
        </fieldset>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal  layui-btn-radius" style="background-color: #d1e3cc"
                        lay-event="batchDel">批量删除
                </button>
                <button class="layui-btn layui-btn-normal  layui-btn-radius" style="background-color: #d1e3cc"
                        lay-event="getCheckLength">获取选中数目
                </button>
                <button class="layui-btn layui-btn-normal  layui-btn-radius" style="background-color: #d1e3cc"
                        lay-event="isAll">验证是否全选
                </button>
                <button class="layui-btn layui-btn-normal  layui-btn-radius" style="background-color: #d1e3cc"
                        lay-event="writeExcel">导出Excel
                </button>
                <button class="layui-btn layui-btn-normal  layui-btn-radius" style="background-color: #d1e3cc"
                        lay-event="writeAll">全部导出
                </button>
                <button class="layui-btn layui-btn-normal  layui-btn-radius" style="background-color: #d1e3cc"
                        lay-event="getExcelTemplate">获取Excel模板
                </button>
                <button type="button" class="layui-btn layui-btn-normal  layui-btn-radius"
                        style="background-color: #d1e3cc" id="test1">
                    <i class="layui-icon">&#xe67c;</i>导入Excel文件
                </button>
            </div>
        </script>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal  layui-btn-radius data-count-edit" style="background-color: #deec4f"
               lay-event="edit"><i class="layui-icon"></i>编辑</a>
            <a class="layui-btn layui-btn-normal  layui-btn-radius data-count-delete" style="background-color: #d7445a"
               lay-event="logic-del"><i class="layui-icon"></i>删除</a>
        </script>


    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        table.render({
            //cellHeight: 300,
            skin: 'nob',// 无边框风格
            // size:'lg',// 大尺寸
            cellMinWidth: 80,
            elem: '#currentTableId',
            url: 'user/findByPage',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: '编号', sort: true, align: "center"},
                {field: 'username', width: 80, title: '账号', align: "center"},
                {field: 'password', width: 80, title: '密码', align: "center"},
                {field: 'nick', width: 80, title: '昵称', align: "center"},
                {field: 'urls', title: '头像', templet: '#img', align: "center"},
                {
                    field: 'time',
                    title: '注册时间',
                    templet: "<div> {{layui.util.toDateString(d.time,'yyyy-MM-dd') }} </div>",
                    align: 'center'
                },
                {field: 'address', title: '地址', align: "center"},
                {field: 'phone', title: '手机号', align: "center"},
                {field: 'state', width: 80, title: '状态', align: "center"},
                {title: '操作', minWidth: 220, toolbar: '#currentTableBar', align: "center"}
            ]],
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'], //自定义分页布局
                limits: [5, 10, 15, 20, 25],
                limit: 5,
                page: true,
                skin: 'line',
                groups: 1 //只显示 1 个连续页码
                , first: "首页"
                , last: "尾页"
            }
        });
        // 监听搜索操作
        var $ = layui.$, active = {
            reload: function () {
                var num = $('#demoReload');
                var condition = $('#select')
                //console.log(username.val());
                //执行重载
                table.reload('currentTableId', {
                    page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                        layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'], //自定义分页布局
                        limits: [5, 10, 15, 20, 25],
                        limit: 5,
                        page: true,
                        skin: 'line',
                        groups: 1 //只显示 1 个连续页码
                        , first: "首页"
                        , last: "尾页"
                    }
                    , where: {
                        v: num.val(),
                        c: condition.val()
                    }
                }, 'data');
            }
        };
        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

//头工具栏事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'batchDel':
                    var data = checkStatus.data;
                    //layer.alert(JSON.stringify(data));
                    var ids = " ";
                    for (var i = 0; i < data.length; i++) {
                        ids += "&ids=" + data[i].id;
                    }
                    $.get("/user/batchDel", ids,
                        function (data) {
                            //alert(data);
                            if ("success" == data) {
                                table.reload("currentTableId", {});//重新加载当前表格
                            }
                        });
                    break;
                case 'writeExcel':
                    var data = checkStatus.data;
                    //layer.msg('选中了：'+ data.length + ' 个');

                    //获取选中数据的id
                    var ids = "";

                    for (var i = 0; i < data.length; i++) {
                        ids += "&ids=" + data[i].id;
                    }

                    //location对象的href属性表示跳转到指定路径
                    location.href = "/user/wirteExcel?ids=" + ids

                    break;
                case 'writeAll':
                    location.href = "/user/writeAll"
                    break;
                case 'getExcelTemplate':
                    //layer.msg(checkStatus.isAll ? '全选': '未全选');

                    location.href = "/excel/user.xlsx"

                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            console.log(data);
            console.log(data.id + "-----------------");
            if (obj.event === 'edit') {
                queryOne(data);
            } else if (obj.event === 'logic-del') {
                //修改操作
                var deleteId = data.id;
                $.get("/user/logicDelete", {id: deleteId},
                    function (data) {
                        if ("success" == data) {
                            table.reload("currentTableId", {});//重新加载当前表格
                        }
                    });
            }
        });


    });
</script>

<script type="text/html" id="img">
    <div><img src="{{d.urls}}" style="object-fit: scale-down;border-radius: 50px"></div>
</script>
<script type="text/html" id="updateUserForm">

    <form class="layui-form" id="updateUser">

        <!-- 把id 作为隐藏域传过去 -->
        <input type="hidden" name="id" id="id">
        <div class="layui-form-item">
            <label class="layui-form-label">账号</label>
            <div class="layui-input-inline">
                <input type="text" name="username" id="username" required lay-verify="required"
                       placeholder="请输入账号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input type="text" name="password" id="password" required lay-verify="required"
                       placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">昵称</label>
            <div class="layui-input-inline">
                <input type="text" name="nick" id="nick" required lay-verify="required" placeholder="请输入年龄"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">注册时间</label>
                <div class="layui-input-inline">
                    <input type="date" name="time" id="time" lay-verify="date" placeholder="yyyy-MM-dd"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">地址</label>
            <div class="layui-input-inline">
                <input type="text" name="address" id="address" required lay-verify="required"
                       placeholder="请输入地址" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-inline">
                <input type="text" name="phone" id="phone" required lay-verify="required" placeholder="请输入手机号"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
    </form>

</script>
<script>
    function queryOne(data) {
        layer.open({
            type: 1,
            content: $("#updateUserForm").html(),
            title: ['修改页面', 'font-size:18px;'],
            area: ['600px', '600px'],
            success: function () {
                $("#id").val(data.id);
                $("#username").val(data.username);
                $("#password").val(data.password);
                $("#nick").val(data.nick);
                $("#time").val(layui.util.toDateString(data.time, 'yyyy-MM-dd'));
                $("#address").val(data.address);
                $("#phone").val(data.phone);
                //渲染页面
                layui.form.render();
            },


            btn: ['提交', '取消']
            , yes: function (index, layero) {

                //修改操作
                $.get("/user/updateUser", $("#updateUser").serialize(),
                    function (data) {
                        if ("success" == data) {
                            location.reload();//刷新当前页面
                        }
                    });

            }
            , btn2: function (index, layero) {
                alert("取消")
            }
        });
    }
</script>
<script>
    layui.use('upload', function () {
        var upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            , accept: 'file'
            , url: '/user/upload' //上传接口
            , done: function (res) {
                if (res.msg == "success") {
                    alert("数据导入成功！！")
                    location.reload();//刷新当前页面
                }
            }
            , error: function () {
                //请求异常回调
            }
        });
    });
</script>
<script>
    layui.use('carousel', function () {
        var carousel = layui.carousel;
        //建造实例
        carousel.render({
            elem: '#test10'
            , width: '100%' //设置容器宽度
            , arrow: 'hover' //始终显示箭头
            , anim: 'fade' //切换动画方式
            , autoplay: true
            , interval: 3000

        });
    });
</script>
</body>
<style>
    .layui-table-cell {
        height: auto;
        /* width: auto;
         min-width: 100px;*/
        line-height: 30px;
    }
</style>
</html>